"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FinchLogger_Color = exports.FinchLogger = void 0;
var fs = require("fs");
var path = require("path");
var finch_tools_1 = require("finch-tools");
/***
 * 定义可绘制的颜色
 */
var FinchLogger_Color;
(function (FinchLogger_Color) {
    FinchLogger_Color[FinchLogger_Color["green"] = 0] = "green";
    FinchLogger_Color[FinchLogger_Color["red"] = 1] = "red";
    FinchLogger_Color[FinchLogger_Color["yellow"] = 2] = "yellow";
    FinchLogger_Color[FinchLogger_Color["yellowBG"] = 3] = "yellowBG";
    FinchLogger_Color[FinchLogger_Color["redBG"] = 4] = "redBG";
})(FinchLogger_Color || (FinchLogger_Color = {}));
exports.FinchLogger_Color = FinchLogger_Color;
/**
 * 打印日志类
 */
var FinchLogger = /** @class */ (function () {
    function FinchLogger(option) {
        this._maxLines = 2000;
        this._maxFiles = 500;
        this._enableConsoleLog = true;
        this._enableFileLog = true;
        this._logStorageDir = "./finch_log";
        this._createNewLogFile = true;
        this._currentWriteStream = undefined;
        this._currentFileLineCount = 0;
        if (!option)
            return;
        if (option.maxFiles)
            this._maxFiles = option.maxFiles;
        if (option.maxFiles)
            this._maxLines = option.maxLines;
        if (option.enableConsoleLog)
            this._enableConsoleLog = option.enableConsoleLog;
        if (option.enableFileLog)
            this._enableFileLog = option.enableFileLog;
        if (option.logStorageDir)
            this._logStorageDir = option.logStorageDir;
    }
    //如果日志总数大于之前设定的日志，则删除多的文件
    FinchLogger.prototype.delLog = function () {
        if (!fs.existsSync(this._logStorageDir)) {
            return;
        }
        var list = fs.readdirSync(this._logStorageDir);
        while (list.length > this._maxFiles) {
            var f = null;
            for (var a in list) {
                if (path.extname(list[a]) === '.txt') {
                    f = list[a];
                    fs.unlinkSync(path.join(this._logStorageDir, f));
                    break;
                }
            }
            list = fs.readdirSync(this._logStorageDir);
        }
    };
    /**
     * 日志API
     * @param message 信息
     * @param color 颜色（可选）
     */
    FinchLogger.prototype.log = function (message, color) {
        var messageString = message;
        var timeStamp = (0, finch_tools_1.getReadableDate)();
        if (this._enableConsoleLog)
            console.log(paint(timeStamp, FinchLogger_Color.yellow) + " : " + paint(messageString, color));
        if (this._enableFileLog) {
            if (!fs.existsSync(this._logStorageDir))
                fs.mkdirSync(this._logStorageDir, { recursive: true });
            if (this._createNewLogFile) {
                this.delLog();
                this._currentWriteStream = fs.createWriteStream(path.join(this._logStorageDir, timeStamp + ".txt"), { flags: 'a' });
                this._createNewLogFile = false;
            }
            if (this._currentWriteStream) {
                var theInfo = timeStamp + ' : ' + message;
                this._currentWriteStream.write(theInfo + '\r\n');
                this._currentFileLineCount++;
                if (this._currentFileLineCount > this._maxLines) {
                    this._currentWriteStream.end();
                    this._createNewLogFile = true;
                    this._currentFileLineCount = 0;
                }
            }
        }
    };
    FinchLogger.prototype.logInfo = function (message) {
        this.log("[---INFO---] : ".concat(message));
    };
    FinchLogger.prototype.logWarning = function (message) {
        this.log("[-WARNING!-] : ".concat(message), FinchLogger_Color.yellow);
    };
    FinchLogger.prototype.logError = function (message) {
        this.log("[--ERROR!--] : ".concat(message), FinchLogger_Color.red);
    };
    return FinchLogger;
}());
exports.FinchLogger = FinchLogger;
/***
 * 绘制颜色的方法
 * @param message 原始信息
 * @param color 增加颜色后的信息
 */
function paint(message, color) {
    var logColorStyler = [
        ['\x1B[32m', '\x1B[39m'],
        ['\x1B[31m', '\x1B[39m'],
        ['\x1B[33m', '\x1B[39m'],
        ['\x1B[43m', '\x1B[49m'],
        ['\x1B[41m', '\x1B[49m']
    ];
    if (color !== undefined) {
        return logColorStyler[color][0] + message + logColorStyler[color][1] + " ";
    }
    else
        return message;
}
